<?php

namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class HomesysController extends AdminbaseController {
    public function index() {
 
        
        $p=1;
        if (isset($_GET['p'])){
            $p=$_GET['p'];
        }
        $queryDesc='order by id';
        
        $postData=array();
        $where='';
        $postData['p']=$p;

        $Model = M();
        
        
        if (isset($_GET['gid'])){
            $_SESSION['gameid']=$_GET['gid'];
        }
        $gameInfo=$Model->query("SELECT id,name FROM `z_game`   ");
        if (isset($_SESSION['gameid'])){
            $queryTable=$Model->query("SELECT id,name FROM `z_game` where id=".$_SESSION['gameid']." limit 0,1 ");
            $tableName='y'.$queryTable[0]['id'];
        }else{
            $queryTable=$Model->query("SELECT id,name FROM `z_game` where is_open=1 limit 0,1 ");
            $tableName='y'.$queryTable[0]['id'];
        }
        $gameId=$queryTable[0]['id'];
        $gameName=$queryTable[0]['name'];
        
       
        
        $postData['name']='';
        $postData['newold']='';
        if (isset($_POST['name']) && !empty($_POST['name'])){
            $where.=" id='".$_POST['name']."' or phone='".$_POST['name']."' or name like'%".$_POST['name']."%'  ";
            $postData['name']=$_POST['name'];
        }
        if (isset($_POST['newold']) && !empty($_POST['newold'])){
            if (empty($where)){
                $where.=" beizhu='".$_POST['newold']."'  ";
            }else{
                $where.=" and beizhu='".$_POST['newold']."'  ";
            }
            $postData['newold']=$_POST['newold'];
        }
        if (!empty($where)){
            $where=' where '.$where;
        }
        
        $result = $Model->query("SHOW TABLES LIKE '".$tableName."_team' ");
        if (empty($result)){
            $tableName='y1'; //表不存在
            $where=" where id='0' ";
        }
        
        $query=$Model->query("SELECT count(id) as count FROM `".$tableName."_users` ".$where."  ");
        $count=$query[0]['count'];
        $page = $this->page($count, 10, $p);
        $uInfo=$Model->query("SELECT * FROM `".$tableName."_users` ".$where."  ".$queryDesc." limit ".(($p-1)*10).",10 ");
        $theData=array();
        foreach ($uInfo as $v){
            $theData[]=array('id'=>$v['id'],'nickname'=>base64_decode($v['nickname']), 'headimg'=>$v['headimg'],
                'phone'=>$v['phone'],'beizhu'=>$v['beizhu'],'createtime'=>$v['createtime']
            );
        }
        $this->assign("postData", $postData);
        $this->assign("page", $page->show('Admin'));
        $this->assign("thedata", $theData);
        $this->assign("gameName", $gameName);
        $this->assign("gameId", $gameId);
        $this->assign("gameInfo", $gameInfo);

        $this->display();
    }
  
    public function edit() {
        if (isset($_GET['id'])){
            $id=$_GET['id'];
        }else{
            exit;
        }
        $p=1;
        if (isset($_GET['p'])){
            $p=$_GET['p'];
        }
        $Model = M();
        
        $queryTable=$Model->query("SELECT id FROM `z_game` where is_open=1 limit 0,1 ");
        $tableName='y'.$queryTable[0]['id'];
        
        $thedata=$Model->query("SELECT id,beizhu,phone,nickname,headimg FROM `".$tableName."_users` where id='".$id."' limit 0,1 ");
        $nickname=base64_decode($thedata[0]['nickname']);
        $thedata[0]['nickname']=$nickname;
        
       
        $this->assign("p", $p);
        $this->assign("thedata", $thedata[0]);
        
        $this->display();
    }
     
    public function editpost() {
        
        if (isset($_POST['beizhu']) && isset($_POST['option_id'])){
            $Model = M();
            $queryTable=$Model->query("SELECT id FROM `z_game` where is_open=1 limit 0,1 ");
            $tableName='y'.$queryTable[0]['id'];
            
            $beizhu=addslashes(trim($_POST['beizhu']));
            if (!empty($beizhu)){
                $Model->execute("update `".$tableName."_users` set beizhu='".$beizhu."'  where id='".$_POST['option_id']."' ");
            }else{
                $Model->execute("update `".$tableName."_users` set `beizhu` = NULL  where id='".$_POST['option_id']."' ");
            }
            
            $info=json_encode(array('status'=>'yes'));
            echo $info;
        }
    }
 
}
